﻿using System;
using System.Collections;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Xml.Linq;
using INTEX.DataBase;
using WPFExcelReport;
using INTEX.DataBase.Tools;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class YCAAUTOFSAB2INTEX : BaseFunction
    {
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD = "100227";

        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        OracleCommand or1;
        public OracleConnection OracleDB;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string ApplicationPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); }
        }
        public YCAAUTOFSAB2INTEX(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {

            OracleDB = _OracleDB;
            proxy = _proxy;
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public bool Execute(XDocument xdoc = null)
        {
            bool flagValidate = false;
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            //helper            
            ITXDATE = date;
            #region============ABKOPF=====================
            int count = 0;
            if (SetData.Tables.Contains("ABKOPF"))
            {
                foreach (DataRow row in SetData.Tables["ABKOPF"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("("+count+")开始创建ABKOPF:" + row["ABKAUFKRS"].ToString() + "_" + row["ABKAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveABKOPF(row);
                }
            }
            #endregion
            string client = "", krs = "", lfd = "", pos = "";
            #region============ABPOS======================
            if (SetData.Tables.Contains("ABPOS"))
            {
                foreach (DataRow row in SetData.Tables["ABPOS"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建ABPOS:" + row["ABPAUFKRS"].ToString() + "_" + row["ABPAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    
                    client = row["ABPMANDANT"].ToString();
                    krs = row["ABPAUFKRS"].ToString();
                    lfd = row["ABPAUFLFD"].ToString();
                    pos = row["ABPAUFPOS"].ToString();
                    INTEXDBC.SaveABPOS(row);
                    INTEXBL.CheckEvents("A001010", row);
                    INTEXBL.CheckEvents("A001011", row);
                    INTEXDBC.DeleteABFOLGE(client, krs, lfd, pos);
                    INTEXDBC.DeleteGAAUF(client, krs, lfd, pos);
                    INTEXDBC.DeleteMDECK(client, krs, lfd, pos);
                    INTEXDBC.DeleteWDECK(client, krs, lfd, pos);
                    INTEXDBC.DeleteFDECK(client, krs, lfd, pos);
                }
            }
            #endregion

            #region============ABPR=======================
            if (SetData.Tables.Contains("ABPR"))
            {
                foreach (DataRow row in SetData.Tables["ABPR"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建ABPR:" + row["ARAUFKRS"].ToString() + "_" + row["ARAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveABPR(row);
                }
            }
            #endregion

            #region============ABAUFM=======================
            if (SetData.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow row in SetData.Tables["ABAUFM"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建ABAUFM:" + row["ABAAUFKRS"].ToString() + "_" + row["ABAAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveABAUFM(row);
                }
            }
            #endregion

            #region============GAAUF=======================
            if (SetData.Tables.Contains("GAAUF"))
            {
                foreach (DataRow row in SetData.Tables["GAAUF"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建GAAUF:" + row["GAAUFKRS"].ToString() + "_" + row["GAAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveGAAUF(row);
                }
            }
            #endregion

            #region============WDECK=======================
            if (SetData.Tables.Contains("WDECK"))
            {
                foreach (DataRow row in SetData.Tables["WDECK"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建WDECK:" + row["WDAUFKRS"].ToString() + "_" + row["WDAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveWDECK(row);
                }
            }
            #endregion

            #region============FDECK=======================
            if (SetData.Tables.Contains("FDECK"))
            {
                foreach (DataRow row in SetData.Tables["FDECK"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建FDECK:" + row["FDAUFKRS"].ToString() + "_" + row["FDAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveFDECK(row);
                }
            }
            #endregion

            #region============MDECK=======================
            if (SetData.Tables.Contains("MDECK"))
            {
                foreach (DataRow row in SetData.Tables["MDECK"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建MDECK:" + row["MDAUFKRS"].ToString() + "_" + row["MDAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveMDECK(row);
                }
            }
            #endregion

            #region============ABFOLGE=======================
            if (SetData.Tables.Contains("ABFOLGE"))
            {
                foreach (DataRow row in SetData.Tables["ABFOLGE"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("(" + count + ")开始创建ABFOLGE:" + row["ABFAUFKRS"].ToString() + "_" + row["ABFAUFLFD"].ToString(), StatusEventType.RunningProgress); count++;
                    INTEXDBC.SaveABFOLGE(row);
                }
            }
            #endregion
            #region============ICC_ABPOSFS=======================
            if (SetData.Tables.Contains("ICC_ABPOSFS"))
            {
                foreach (DataRow row in SetData.Tables["ICC_ABPOSFS"].Rows)
                {
                    INTEXDBC.SaveICC_ABPOSFS(row);
                }
            }
            #endregion
            #region============ICC_ABPOS_MCO=======================
            if (SetData.Tables.Contains("ABKOPF"))
            {
                foreach (DataRow row in SetData.Tables["ABKOPF"].Rows)
                {
                    INTEXDBC.InsertICC_ABPOS_MCO(row, 3, "");
                    INTEXDBC.InsertICC_ABPOS_MAT(row, 3, "");
                }
            }
            #endregion
            return true;
        }

        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}